package ${bean.packageName};

import ${bean.importPackage}.model.${bean.className};
import ${bean.importPackage}.service.${bean.className}Service;
import com.solution.framework.controller.BaseController;
import com.solution.framework.util.MessageDefined;
import com.solution.framework.util.MessageDto;
import com.solution.framework.util.Pagination;
import com.solution.framework.util.RequestVarsUtil;
import org.hibernate.criterion.Criterion;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* Created with generator tools.
* User: ${bean.createUser}
* DateTime:${bean.createTime}
*/
@Controller
public class ${bean.className}Controller extends BaseController<${bean.className}> {

private static final String path_index = "${bean.lowerClassName}/${bean.lowerClassName}_index";
private static final String path_input = "${bean.lowerClassName}/${bean.lowerClassName}_input";
private static final String path_view = "${bean.lowerClassName}/${bean.lowerClassName}_view";

@Resource
private ${bean.className}Service ${bean.lowerClassName}Service;

@RequestMapping
public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("${bean.lowerClassName} index method ....");
        Pagination pagination = RequestVarsUtil.getPagination(request);
        List<Criterion> criterions = this.getSearchCriterions(request);
        List<${bean.className}> entitys = this.${bean.lowerClassName}Service.listByCriteria(pagination, criterions);
        int total = this.${bean.lowerClassName}Service.getTotalByCriteria(criterions);
        RequestVarsUtil.setPagination(pagination, total);
        request.setAttribute("pageModel", pagination);
        model.addAttribute("list", entitys);
        return path_index;
}

    @RequestMapping
    public String edit(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("${bean.lowerClassName} edit method ....");
        ${bean.className} ${bean.lowerClassName} = this.${bean.lowerClassName}Service.get(id);
        model.addAttribute("${bean.lowerClassName}", ${bean.lowerClassName});
        return path_input;
    }

    @RequestMapping
    public String add(Long bizId, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("${bean.lowerClassName} add method ....");
        ${bean.className} ${bean.lowerClassName} = new ${bean.className}();
        model.addAttribute("${bean.lowerClassName}", ${bean.lowerClassName});
        return path_input;
    }

    @RequestMapping
    public String view(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("${bean.lowerClassName} view method ....");
        ${bean.className} ${bean.lowerClassName} = this.${bean.lowerClassName}Service.get(id);
        model.addAttribute("${bean.lowerClassName}", ${bean.lowerClassName});
        return path_view;
    }

    @ResponseBody
    @RequestMapping
    @DateTimeFormat
    public void save(${bean.className} ${bean.lowerClassName}, HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    System.out.println("${bean.lowerClassName} save method ....");
    MessageDto messageDto = new MessageDto();
    try {
    if (${bean.lowerClassName}.getId()!=null) {
    this.${bean.lowerClassName}Service.update(${bean.lowerClassName});
    super.setMessageDto(messageDto, MessageDefined.update_success_info);
    } else {
    this.${bean.lowerClassName}Service.save(${bean.lowerClassName});
    super.setMessageDto(messageDto, MessageDefined.save_success_info);
    }
    } catch (Exception e) {
    messageDto.setSuccess(false);
    super.setMessageDto(messageDto, MessageDefined.sys_error_info);
    e.printStackTrace();
    }
    super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void delete(String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
    MessageDto messageDto = new MessageDto();
    try {
    Integer count = this.${bean.lowerClassName}Service.delete(ids);
    super.setMessageDto(messageDto, MessageDefined.del_success_info);
    } catch (Exception e) {
    messageDto.setSuccess(false);
    super.setMessageDto(messageDto, MessageDefined.sys_error_info);
    e.printStackTrace();
    }
    super.writeToClient(response, messageDto.toString());
    }
}
